//
//  CZNetWorkingTools.swift
//  AFN网络封装
//
//  Created by 左凯旭 on 16/8/10.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import AFNetworking


/*
 - 对afn进行封装
 - 提供一个可以发送get和post请求的公共方法
 - 帮助当前app 请求所有接口数据
 */

enum CZNetWorkingToolsMethod:String {
    case GET = "GET"
    case POST = "POST"
}


class CZNetWorkingTools: AFHTTPSessionManager {

    //建立一个单例,全局访问点
    static let sharedTools:CZNetWorkingTools = {
       
        let Tools = CZNetWorkingTools()
        Tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return Tools
        
    }()
    
    
    /**
     网络请求公共方法
     
     - parameter method:     请求方式
     - parameter urlString:  请求urlString
     - parameter parameters: 请求参数
     - parameter success:    请求成功的闭包
     - parameter failure:    请求失败的闭包
     */
    func request(method:CZNetWorkingToolsMethod,urlString:String,parameters:AnyObject?,succes:(response:AnyObject?)->(),failure:(error:NSError)->()){
        
        if method == .GET {
            
            GET(urlString, parameters: parameters, progress: nil, success: { (_, response) in
                
                succes(response:response)
                
                }, failure: { (_, error) in
                    
                  failure(error: error)
            })
            
        }else{
            
            POST(urlString, parameters: parameters, progress: nil, success: { (_, response) in
                
                succes(response:response)
                
                }, failure: { (_, error) in
                
                    failure(error: error)
            })
            
        }
        
        
    }
    
    
}
//MARK: - 发布微博界面请求
extension CZNetWorkingTools{
    
    //发布文字微博
    func composeUpdate(status:String,succes:(response:AnyObject?)->(),failure:(error:NSError)->()){
        
            let urlStr = "https://upload.api.weibo.com/2/statuses/update.json"
            //参数
            let param = [
                "access_token": CZUserAccountViewModel.shareTools.accessToken!,
                "status": status
            ]
        //发送请求
        request(CZNetWorkingToolsMethod.POST, urlString: urlStr, parameters: param, succes: succes, failure: failure)
        
    }
    
   
    
    //发布图片微博
    func composeUpload(status:String,images:[UIImage],success:(response:AnyObject?)->(),failure:(error:NSError)->()){
        
        
        let urlStr = "https://upload.api.weibo.com/2/statuses/upload.json"
        
        let params = [
            "access_token": CZUserAccountViewModel.shareTools.accessToken!,
            "status": status
        ]
        
        //发送请求
        POST(urlStr, parameters: params, constructingBodyWithBlock: { (formdata) in
            /**
             *  上传文件
             *
             *  @param 要上传的文件二进制
             *  @param 服务器提供的名字
             *  @param 保存到服务器指定文件夹的名字(路径) 但是 一般客户端不需要指定 可以随意填写
             *  @param 上传的文件的格式
             *
             *  @return
             */
            let data = UIImagePNGRepresentation(images.first!)!
            
             formdata.appendPartWithFileData(data, name: "pic", fileName: "yxyx", mimeType: "application/octet-stream")
            
            }, progress: nil, success: { (_, response) in
                success(response: response)
            }) { (_, error) in
                failure(error: error)
        }
        
    }
    
    
    
}

//MARK: - 微博首页数据
extension CZNetWorkingTools{
    
    func loadHomeData(sinceId:Int64,maxId:Int64,succes:(response:AnyObject?)->(),failure:(error:NSError)->()){
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let param = [
            "access_token":CZUserAccountViewModel.shareTools.userAccount!.access_token!,
            "since_id":"\(sinceId)",
            "max_id":"\(maxId)"
        ]
        
        
        request(CZNetWorkingToolsMethod.GET, urlString: urlString, parameters: param, succes: succes, failure: failure)
     
    }
    
  
}




//授权界面接口
extension CZNetWorkingTools{
    //请求授权
    func loadUserAccount(code:String,succes:(response:AnyObject?)->(),failure:(error:NSError)->()){
        
        //请求urlSring
        let urlString = "https://api.weibo.com/oauth2/access_token"
        //请求参数
        let param = [
            
            "client_id":WBAppKey,
            "client_secret":WBAppSecret,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":WBredirect_uri
        ]
        
        request(CZNetWorkingToolsMethod.POST, urlString: urlString, parameters: param, succes: succes, failure: failure)
        
    }
    
    
    //请求用户数据
    func loadUserInfo(userAccount:CZUserAccountModel,succes:(response:AnyObject?)->(),failure:(error:NSError)->()){
        let urlString = "https://api.weibo.com/2/users/show.json"
        let param = [
            
            "access_token":userAccount.access_token!,
            "uid":userAccount.uid!
        ]
        
        request(CZNetWorkingToolsMethod.GET, urlString: urlString, parameters: param, succes: succes, failure: failure)
        
    }
    
    
}

















